/**
 * 
 */
package org.xlab.baseframework.test.platform.webservice.impl;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import javax.jws.WebService;

import org.apache.log4j.Logger;
import org.xlab.baseframework.exception.ObjectNotFoundException;
import org.xlab.baseframework.exception.SystemException;
import org.xlab.baseframework.test.platform.domain.BizTransactionLog;
import org.xlab.baseframework.test.platform.domain.enumerate.EnumUploadedTransactionResponse;
import org.xlab.baseframework.test.platform.domain.enumerate.SynchronizedInditify;
import org.xlab.baseframework.test.platform.service.api.IBizTransactionLogService;
import org.xlab.baseframework.test.platform.webservice.api.ITransactionWebService;

/**
 * @author makubex
 * 
 */
@WebService(endpointInterface = "org.xlab.baseframework.test.platform.webservice.api.ITransactionWebService")
public class TransactionWebServiceImpl implements ITransactionWebService {

	private static final Logger log = Logger
			.getLogger(TransactionWebServiceImpl.class);

	private IBizTransactionLogService bizTransactionLogService;

	/**
	 * @return the bizTransactionLogService
	 */
	public IBizTransactionLogService getBizTransactionLogService() {
		return bizTransactionLogService;
	}

	/**
	 * @param bizTransactionLogService
	 *            the bizTransactionLogService to set
	 */
	public void setBizTransactionLogService(
			IBizTransactionLogService bizTransactionLogService) {
		this.bizTransactionLogService = bizTransactionLogService;
	}

	/**
	 * @see org.xlab.baseframework.test.platform.webservice.api.ITransactionWebService#upload(java.util.Collection)
	 */
	public Hashtable<String, EnumUploadedTransactionResponse> upload(
			BizTransactionLog[] transactions) throws SystemException {
		Hashtable<String, EnumUploadedTransactionResponse> results = new Hashtable<String, EnumUploadedTransactionResponse>();
		for (BizTransactionLog trans : transactions) {
			try {
				BizTransactionLog exampleInstance = new BizTransactionLog();
				exampleInstance.setToken(trans.getToken());
				exampleInstance.setDeviceId(trans.getDeviceId());
				List<BizTransactionLog> records = bizTransactionLogService
						.findAllByExample(exampleInstance);
				if (records.get(0).getSynchronizedInd() == SynchronizedInditify.Y)
					results.put(
							trans.getToken(),
							EnumUploadedTransactionResponse.ALREADY_SYNCHRONIZED);
				else
					results.put(trans.getToken(),
							EnumUploadedTransactionResponse.ALREADY_UPLOADED);
				log.error("Token conflicted: " + trans.getToken());
				log.warn("Go to the next row");
			} catch (ObjectNotFoundException e) {
				trans.setSynchronizedInd(SynchronizedInditify.N);
				bizTransactionLogService.save(trans);
				results.put(trans.getToken(),
						EnumUploadedTransactionResponse.UPLOAD_SUCCEED);
			}
		}
		return results;
	}

	/**
	 * @see org.xlab.baseframework.test.platform.webservice.api.ITransactionWebService#upload(java.lang.String[],
	 *      java.lang.String[], java.lang.String[], java.lang.String[],
	 *      java.lang.String[], java.lang.Integer[], java.lang.String[])
	 */
	public String[][] upload(String[] commandId, String[] deviceId,
			String[] token, String[] epcNo, String[] location, Integer[] qty,
			String[] lotNo) throws SystemException {
		String[][] results = new String[commandId.length][2];
		for (int i = 0; i < commandId.length; i++) {
			try {
				BizTransactionLog exampleInstance = new BizTransactionLog();
				exampleInstance.setToken(token[i]);
				exampleInstance.setDeviceId(deviceId[i]);
				List<BizTransactionLog> records = bizTransactionLogService
						.findAllByExample(exampleInstance);
				results[i][0] = token[i];
				if (records.get(0).getSynchronizedInd() == SynchronizedInditify.Y)
					results[i][1] = EnumUploadedTransactionResponse.ALREADY_SYNCHRONIZED
							.toString();
				else
					results[i][1] = EnumUploadedTransactionResponse.ALREADY_UPLOADED
							.toString();
				log.error("Token conflicted: " + token[i]);
				log.warn("Go to the next row");
			} catch (ObjectNotFoundException e) {
				BizTransactionLog bizLog = new BizTransactionLog();
				bizLog.setCommandId(commandId[i]);
				bizLog.setDeviceId(deviceId[i]);
				bizLog.setToken(token[i]);
				bizLog.setEpcNo(epcNo[i]);
				bizLog.setLocation(location[i]);
				bizLog.setQty(qty[i]);
				bizLog.setLotNo(lotNo[i]);
				bizLog.setSynchronizedInd(SynchronizedInditify.N);
				bizLog.setCreatedTime(new Date());
				bizLog.setLastUpdatedTime(new Date());
				bizTransactionLogService.save(bizLog);
				results[i][0] = token[i];
				results[i][1] = EnumUploadedTransactionResponse.UPLOAD_SUCCEED
						.toString();
			}
		}
		return results;
	}

}
